package com.dpiinc.TechPage.utils

import java.text.SimpleDateFormat
import java.util.*

/**
 * 日期工具类
 * Created by X on 2018/6/17.
 */
@Suppress("unused")
object DateUtils {
    fun getCurrentTimeMillis(): Long {
        return System.currentTimeMillis()
    }

    fun getDateString(formatString: String): String {
        return getDateString(getCurrentTimeMillis(), formatString)
    }

    fun getDateString(time: Long, formatString: String): String {
        val format = SimpleDateFormat(formatString, Locale.CHINA)
        val date = Date(time)
        return format.format(date)
    }

    /**
     * 返回格式化之后的时长字符串,如45:39,01:45:39
     */
    fun getDurationString(time: Long, timeFormatStr: String): String {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.set(Calendar.MINUTE, 0)
        calendar.set(Calendar.SECOND, 0)
        calendar.set(Calendar.MILLISECOND, 0)
        val timeInMillis = calendar.timeInMillis + time
        return DateUtils.getDateString(timeInMillis, timeFormatStr)
    }

    fun getNoteDateString(time: Long): String {
        val date = Date(time)
        val cal = Calendar.getInstance()
        cal.time = date
        val year = cal.get(Calendar.YEAR)
        val day = cal.get(Calendar.DAY_OF_YEAR)
        val curDate = Calendar.getInstance(Locale.CHINA)
        curDate.timeInMillis = getCurrentTimeMillis()
        val curYear = curDate.get(Calendar.YEAR)
        val curDay = curDate.get(Calendar.DAY_OF_YEAR)
        val sdf = if (year == curYear && day == curDay) {
            SimpleDateFormat("HH:mm", Locale.getDefault())
        } else {
            SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        }
        return sdf.format(time)
    }
}